<template>
    <div>
        <el-row :gutter="5">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:300px;">
                    <img src="static/img/img.jpg" class="user-avator" alt="">
                    <div> 欢迎您，{{role}}</div>
                    <div class="user-info-list"><!-- {{province}}，{{city}} --></div>
                    <div class="right-div" >
                        <!-- <div style="float:left">
                             <img :src="weatherImg">
                             <div >
                                <span>{{wendu}}<small> ℃</small></span><span>{{weatherType}}</span></div>
                             </div> -->
                         <div style="float:right">
                             <h1>{{ new Date() | moment("HH:mm") }}</h1>
                             <p>{{ new Date() | moment("YYYY-MM-DD dddd") }}</p>
                        </div>
                    </div>
                </el-card>
            </el-col>

            <el-col :span="8">
                    <el-card shadow="hover" class="mgb20" style="height:300px;position: relative;">
                        <el-tabs value="first" :stretch="true" @tab-click="handleClick" >
                            <el-tab-pane label="昨日数据" name="first"></el-tab-pane>
                            <el-tab-pane label="本月数据" name="second"></el-tab-pane>
                            <el-tab-pane label="上月数据" name="third"></el-tab-pane>
                            <el-tab-pane label="总数据" name="fourth"></el-tab-pane>
                        </el-tabs> 
                         <el-row>
                            <el-col :span="24">  
                                <div style="height:160px;position: relative; text-align:center; line-height: 160px;">
                                    <div style="float:left; width:33%;text-align:center; border-right:1px solid #e3e7ea;">
                                        <p style="height:40px;position: relative; text-align:center; line-height: 10px;">
                                          <i class="el-icon-circle-plus" style="font-size: 30px;color: #ffc300"></i>
                                        </p>
                                        <p style="height:40px;position: relative; text-align:center; line-height: 10px;">打款金额</p>
                                        <p style="height:30px;position: relative; text-align:center; line-height: 10px;"><span>{{getAmount}}</span>元</p>
                                    </div>

                                     <div style="float:left; width:33%;text-align:center; border-right:1px solid #e3e7ea; ">
                                        <p style="height:40px;position: relative; text-align:center; line-height: 15px;">
                                          <i class="el-icon-remove" style="font-size: 30px;color: #ffc300"></i></p>
                                        <p style="height:40px;position: relative; text-align:center; line-height: 15px;">发货金额</p>
                                        <p style="height:30px;position: relative; text-align:center; line-height: 15px;">
                                          <span>{{sendAmount}}</span>元
                                        </p>
                                    </div>
                                    <div style="height:160px;position: relative; text-align:center; line-height: 160px; ">
                                        <p style="height:40px;position: relative; text-align:center; line-height: 70px;">
                                          <i class="el-icon-circle-check" style="font-size: 30px;color: #ffc300"></i>
                                        </p>
                                        <p style="height:40px;position: relative; text-align:center; line-height: 40px;">剩余金额</p>
                                        <p style="height:30px;position: relative; text-align:center; line-height: 40px;">
                                          <span>{{getAmount-sendAmount}}</span>元
                                        </p>
                                    </div>
                                </div>
                            </el-col>
                         </el-row>
                    </el-card>
            </el-col>


            <el-col :span="8">
                    <el-card shadow="hover" class="mgb20" style="height:300px;">
                        <div style=" line-height: 37px;border-bottom:2px solid #e3e7ea;">公告栏</div>
                        <div class="announcement-content" style="margin-top:30px;">
                            <div> <span></span><a href="">首页功能待实现             1-7</a></div>
                            <div> <span></span><a href="">基础功能已完成            1-6</a></div>
                            <div> <span></span><a href="">部分功能待财务确认             1-5</a></div>
                        </div>
                    </el-card>
            </el-col>
        </el-row>

        <el-row :gutter="20">
            <el-col :span="24">
                <el-card shadow="hover">
                    <el-tabs value="first" style="float:right;line-height:55px;" @tab-click="schartClick" tab-position="top">
                        <el-tab-pane label="最近7天" name="first"></el-tab-pane>
                        <el-tab-pane label="最近30天" name="second"></el-tab-pane>
                        <el-tab-pane label="最近60天" name="third"></el-tab-pane>
                    </el-tabs>
                 </el-card>
            </el-col>
        </el-row>
        <el-row :gutter="20" style="margin-top:-99px">
               <el-col :span="12">
                    <el-card shadow="hover">
                        <div><schart ref="bar" class="schart" canvasId="bar" :data="data" type="bar" :options="options"></schart></div>
                    </el-card>
                </el-col>
                <el-col :span="12">
                    <el-card shadow="hover">
                        <schart ref="line" class="schart" canvasId="line" :data="dataTwo" type="line" :options="options2"></schart>
                    </el-card>
                </el-col>
        </el-row>
    </div>
</template>

<script>
    import Schart from 'vue-schart';
    import bus from '@/components/common/bus';

    import {parseTime} from '@/utils';

    import { queryWeather,queryCity } from '@/api/tianqi';

    import { selectProBytime,selectBytime,selectShiBytime} from '@/api/payment';

    import {GetCurrWeekStr,GetCurrMouthStr,GetLastWeekStr,GetLastMouthStr,GetBeforOrAfterDayStr} from '@/utils/timeUtils';
    
    //统计
    // import { countOrderGroup,countOrder} from '@/api/homePage';

    //天气
    import {getWeatherInfo} from '@/utils/weatherUtil';
    import {getWeatherLogo} from '@/utils/weatherUtil';
    
    export default {
        name: 'dashboard',
        
        data() {
            return {
                name: localStorage.getItem('ms_username'),
                data: [{
                        name: '',
                        value: 0
                    }
                ],
                 dataTwo: [{
                        name: '',
                        value: 0
                    }
                ],
                options: {
                    title: '最近7天收款金额',
                    showValue: true,
                    fillColor: 'rgb(45, 140, 240)',
                    bottomPadding: 30,
                    topPadding: 30
                },
                options2: {
                    title: '最近7天发货金额',
                    fillColor: '#FC6FA1',
                    axisColor: '#008ACD',
                    contentColor: '#EEEEEE',
                    bgColor: '#F5F8FD',
                    bottomPadding: 30,
                    topPadding: 30
                },
                cityCode:'101280601',
                wendu:'',
                shidu:'',
                weatherImg:'',
                weatherType:'',
                quality:'',
                sendAmount:'1',
                getAmount:'1',
                city:'',
                province:'',
                tianqiDiv:'',
                displayStyle:'display:none'
                ,
            filters:{
              capitalize: function (value) {
                if (!value) return ''
                value = value.toString()
                return value.charAt(0).toUpperCase() + value.slice(1)
             }

            }
        }
        },
        components: {
            Schart
        },
        computed: {
            role() {
                // return this.name === 'admin' ?  : '普通用户';
                 if(this.$store.getters.roles.indexOf("admin") >= 0){
                   return '超级管理员';
                 }else if(this.$store.getters.roles.indexOf("finance") >= 0){
                   return '财务人员';
                 }else{
                   return '合作商';
                 }
            }
        },
        created(){
            console.log(GetCurrMouthStr(1)+' 00:00:00' +GetCurrMouthStr(2)+' 23:59:59')
           
            var startTime =GetBeforOrAfterDayStr(-6)+' 00:00:00';
            var endTime =GetBeforOrAfterDayStr(0)+' 23:59:59';
            this.selectBytime(GetBeforOrAfterDayStr(-1)+' 00:00:00',GetBeforOrAfterDayStr(-1)+' 23:59:59');
            // this.initCityCode();
            // this.initCity();
            this.handleListener();
            this.changeDate();
           
            //默认加载数据
            this.loadOrderCountChart(startTime,endTime);
        },
        activated(){
            this.handleListener();
        },
        deactivated(){
            window.removeEventListener('resize', this.renderChart);
            bus.$off('collapse', this.handleBus);
        },
        methods: {
            changeDate(){
                const now = new Date().getTime();
                this.data.forEach((item, index) => {
                    const date = new Date(now - (6 - index) * 86400000);
                    item.name = `${date.getFullYear()}/${date.getMonth()+1}/${date.getDate()}`
                })
            },
            getWeather(){
                queryWeather(this.cityCode).then(response =>{
                    this.wendu = response.data.data.wendu
                    this.shidu = response.data.data.shidu
                    this.quality =response.data.data.quality
                })
            },
            handleListener(){
                bus.$on('collapse', this.handleBus);
                // 调用renderChart方法对图表进行重新渲染
                window.addEventListener('resize', this.renderChart)
            },
            handleBus(msg){
                setTimeout(() => {
                    this.renderChart()
                }, 300);
            },
            renderChart(){
                this.$refs.bar.renderChart();
                this.$refs.line.renderChart();
            },
            schartClick(tab, event){
                switch(tab.name)
                {
                case "first":
                  //最近七天
                  this.options.title="最近7天收款金额";
                  this.options2.title="最近7天发货金额";
                  var startTime =GetBeforOrAfterDayStr(-7)+' 00:00:00';
                  var endTime =GetBeforOrAfterDayStr(-1)+' 23:59:59';
                  this.loadOrderCountChart(startTime,endTime);
                  break;
                case "second":
                  //最近30天
                  this.options.title="最近30天收款金额";
                  this.options2.title="最近30天发货金额";
                  var startTime =GetBeforOrAfterDayStr(-30)+' 00:00:00';
                  var endTime =GetBeforOrAfterDayStr(-1)+' 23:59:59';
                  this.loadOrderCountChart(startTime,endTime);
                  break;
                case "third":
                  //最近60天
                  this.options.title="最近60天收款金额";
                  this.options2.title="最近60天发货金额";
                  var startTime =GetBeforOrAfterDayStr(-60)+' 00:00:00';
                  var endTime =GetBeforOrAfterDayStr(-1)+' 23:59:59';
                  this.loadOrderCountChart(startTime,endTime);
                  break;
                default:
                  console.log("选择类型错误");
                }
            },
            handleClick(tab, event) {
                switch(tab.name)
                {
                case "first":
                  var startTime =GetBeforOrAfterDayStr(-1)+' 00:00:00';
                  var endTime =GetBeforOrAfterDayStr(-1)+' 23:59:59';
                  this.selectBytime(startTime,endTime);
                  break;
                case "second":
                  var startTime =GetCurrMouthStr(1)+' 00:00:00';
                  var endTime =GetCurrMouthStr(2)+' 23:59:59';
                  this.selectBytime(startTime,endTime);
                  break;
                case "third":
                  var startTime =GetLastMouthStr(1)+' 00:00:00';
                  var endTime =GetLastMouthStr(2)+' 23:59:59';
                  this.selectBytime(startTime,endTime);
                  break;
                case "fourth":
                  this.selectBytime();
                  break;
                default:
                  console.log(5);
                }
            },
            initCityCode(){
               /**获取天气对应的code**/
               var cityCode = localStorage.getItem('CITY_CODE'); 
               if(!cityCode){
                  queryCity().then(response =>{
                     if(response.status==200){
                        localStorage.setItem('CITY_CODE', JSON.stringify(response.data));
                     }
                  }).catch(error=>{
                      console.log(error)
                  })
               }
            },
            initCity(){
                //* 获取IP对应的地址信息（缓存6H）
                var cacheDuration = 21600000; // 6小时的毫秒值
                var localInfo = {}, localInfoStr = localStorage.getItem('LOCATION_INFO'); // 缓存的地址信息
            
                // 根据IP地址获取地理位置（搜狐API）
                if(!localInfoStr){
                    var cname = returnCitySN["cname"];
                    var myGeo = new BMap.Geocoder();
                    if(cname){
                        myGeo.getPoint(cname, function(point){
                            if (point) {
                                myGeo.getLocation(point,function(rs){
                                    if(rs){
                                        var data = rs.addressComponents;
                                        console.info(data);
                                        localInfo.province = data.province;
                                        localInfo.city = data.city;
                                        var localData = {}; // 存储取得的地址信息
                                        localData.ip = localInfo;
                                        localData.time = new Date().getTime() + cacheDuration;
                                        localStorage.setItem('LOCATION_INFO', JSON.stringify(localData));
                                        //根据城市加载天气信息
                                        this.loadWeather(localInfo);
                                    }
                                })
                            }
                        });
                    }
                }else if(localInfoStr){
                     var local = JSON.parse(localInfoStr);
                     localInfo.province = local.ip.province;
                     localInfo.city = local.ip.city;
                     //根据城市加载天气信息
                     this.loadWeather(localInfo);
                }
            },
            loadWeather: function(localInfo){
                // 设置城市位置信息并查询当地天气
                if(localInfo.province){
                    // 显示当前所在省市
                    this.province = localInfo.province;
                    this.city = localInfo.city;
                    var weekArray = JSON.parse(localStorage.getItem('CITY_CODE'));
                    for (var i = 0; i < weekArray.length; i++) {
                      if(localInfo.city.replace('市','')==weekArray[i].city_name){
                        this.cityCode = weekArray[i].city_code;
                      }
                    }
                    //** 获取当前地址信息
                    var weather = getWeatherInfo(this.cityCode); //localInfo.city
                    //** 初始化天气模块
                    this.initWeatherWidget(weather);
                }
            },
            initWeatherWidget: function(weather){ /* 初始化显示天气模块 */
                if(weather && weather.data){
                    // 未来天气信息（包括今天）
                    var forecast = weather.data.forecast;
                    // 取得当前天气的图标
                    var todayImg = getWeatherLogo(forecast[0].type), // {hover:"", image:""}
                    tomorrowImg = getWeatherLogo(forecast[1].type),
                    afterTomorrowImg = getWeatherLogo(forecast[2].type);
                    // 设置要显示的天气内容
                    this.weatherImg =todayImg.hover;
                    this.wendu = weather.data.wendu;
                    this.weatherType = forecast[0].type;
                }
            },
            loadOrderCountChart :function(startTime,endTime){
                try{
                    this.getChartDataByTimeAndType(startTime,endTime,"CUSTOMER_SERVICE_UNDO");
                    this.selectShiBytime(startTime,endTime);
                }catch (e) {
                     console.log(e.name + ":" + e.message);
                }
            },
            selectShiBytime : function(startTime,endTime){
                  selectShiBytime(startTime,endTime).then(response =>{
                      if (response.status == 200 && response.data.status == "success") {
                          if(response.data  && response.data.data.length>0){
                              var lists=[{}];
                              debugger
                              for(var i in response.data.data) {
                                var char = {name:response.data.data[i].time,value:response.data.data[i].sendAmount};
                                lists[i]=char;
                              }
                              this.dataTwo=lists;
                          }else{
                               this.dataTwo = [{ name: new Date().getFullYear()+"-"+new Date().getMonth()+"-"+new Date().getDate(),value: 0}];
                          }
                      }
                  }).catch(() => {
                     console.log("请求失败！");
                  });
            },
            getChartDataByTimeAndType: function(startTime, endTime,orderStatusRangeType){
                selectProBytime(startTime,endTime).then(response =>{
                    if (response.status == 200 && response.data.status == "success") {
                        if(response.data  && response.data.data.length>0){
                            var lists=[{}];
                            for(var i in response.data.data) {
                              var char = {name:parseTime(response.data.data[i].time,'{y}-{m}-{d}'),value:response.data.data[i].getAmount};
                              lists[i]=char;
                            }
                            this.data=lists;
                        }else{
                             this.data = [{ name: new Date().getFullYear()+"-"+new Date().getMonth()+"-"+new Date().getDate(),value: 0}];
                        }
                    }
                }).catch(() => {
                   console.log("请求失败！");
                });
            },
            selectBytime : function(startTime, endTime){
                selectBytime(startTime,endTime).then(response =>{
                     if (response.status == 200 && response.data.status == "success" ) {
                        if(response.data.data){
                            if(response.data.data[0].sendAmount){
                                this.sendAmount=response.data.data[0].sendAmount;
                            }else{
                              this.sendAmount=0;
                            }
                            if(response.data.data[0].getAmount){
                                 this.getAmount=response.data.data[0].getAmount;
                            }else{
                              this.getAmount=0;
                            }
                         
                        }else{
                          this.sendAmount=0;
                          this.getAmount=0;
                        }
                     }
                }).catch(() =>{
                    this.sendAmount=0;
                    this.getAmount=0;
                    console.log("请求失败！");
                });
            }
        }
    }
</script>

<style>
     ul,li {list-style:none;}
     body{
            font-family: "microsoft yahei";
            font-size: 14px;
            line-height: 1.5;
            color: #516472;
     }
     .index .con-box1 .box-fff{
            height: 210px;
            overflow: hidden;
     }
   
   .announcement-content span{
        background: #2FB26A;
        height: 5px;
        width: 5px;
        border-radius: 5px;
        display: inline-block;
        margin-right: 10px;
        margin-bottom: 2px;
   }
   
   .announcement-content div{
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        padding-right: 16px;
        box-sizing: border-box;
        padding-bottom: 10px;
        box-sizing: border-box;
        font-size: 13px;
   }

    .el-row {
        margin-bottom: 0px;
    }

    .grid-content {
        display: flex;
        align-items: center;
        height: 100px;
    }

    .grid-cont-right {
        flex: 1;
        text-align: center;
        font-size: 14px;
        color: #999;
    }

    .grid-num {
        font-size: 30px;
        font-weight: bold;
    }

    .grid-con-icon {
        font-size: 50px;
        width: 100px;
        height: 100px;
        text-align: center;
        line-height: 100px;
        color: #fff;
    }

    .grid-con-1 .grid-con-icon {
        background: rgb(45, 140, 240);
    }

    .grid-con-1 .grid-num {
        color: rgb(45, 140, 240);
    }

    .grid-con-2 .grid-con-icon {
        background: rgb(100, 213, 114);
    }

    .grid-con-2 .grid-num {
        color: rgb(45, 140, 240);
    }

    .grid-con-3 .grid-con-icon {
        background: rgb(242, 94, 67);
    }

    .grid-con-3 .grid-num {
        color: rgb(242, 94, 67);
    }

    .user-info {
        display: flex;
        align-items: center;
        padding-bottom: 20px;
        border-bottom: 2px solid #ccc;
        margin-bottom: 20px;
    }

    .user-avator {
        width: 120px;
        height: 120px;
        border-radius: 50%;
    }

    .user-info-cont {
        padding-left: 50px;
        flex: 1;
        font-size: 14px;
        color: #999;
    }

    .user-info-cont div:first-child {
        font-size: 30px;
        color: #222;
    }

    .user-info-list {
        font-size: 14px;
        color: #999;
        line-height: 25px;
    }

    .user-info-list span {
        margin-left: 70px;
    }

    .mgb20 {
        margin-bottom: 20px;
    }

    .todo-item {
        font-size: 14px;
    }

    .todo-item-del {
        text-decoration: line-through;
        color: #999;
    }

    .schart {
        width: 100%;
        height: 300px;
    }

   div p span{
    font-size: 20px; 
    font-weight: bold;
   }
   
</style>
